
Imports System.IO
Imports Lucene.Net.Analysis


Namespace Aricie.DNN.Modules.LuceneSearch.Business
    Public Class FilteredAnalyzer
        Inherits Analyzer


        Private _Analyzer As Analyzer


        Private _MaxLength As Integer
        Private _MinLength As Integer
        Private _FilterAccents As Boolean
        Private _FilterToLower As Boolean
        Private _StopWords As String()

        Public Property MinLength() As Integer
            Get
                Return _MinLength
            End Get
            Set(ByVal value As Integer)
                _MinLength = value
            End Set
        End Property

        Public Property MaxLength() As Integer
            Get
                Return _MaxLength
            End Get
            Set(ByVal value As Integer)
                _MaxLength = value
            End Set
        End Property

        Public Property FilterAccents() As Boolean
            Get
                Return _FilterAccents
            End Get
            Set(ByVal value As Boolean)
                _FilterAccents = value
            End Set
        End Property

        Public Property FilterToLower() As Boolean
            Get
                Return _FilterToLower
            End Get
            Set(ByVal value As Boolean)
                _FilterToLower = value
            End Set
        End Property

        Public Property StopWords() As String()
            Get
                Return _StopWords
            End Get
            Set(ByVal value As String())
                _StopWords = value
            End Set
        End Property


        Public Sub New(ByVal sourceAnalyzer As Analyzer, Optional ByVal minLength As Integer = 0, _
                        Optional ByVal maxlength As Integer = Integer.MaxValue, _
                        Optional ByVal filterAccents As Boolean = False, Optional ByVal filterToLower As Boolean = False, _
                        Optional ByVal stopWords As String() = Nothing)
            Me._Analyzer = sourceAnalyzer
            Me._MinLength = minLength
            Me._MaxLength = maxlength
            Me._FilterAccents = FilterAccents
            Me._FilterToLower = FilterToLower
            Me._StopWords = StopWords
        End Sub

        Public Overrides Function TokenStream(ByVal fieldName As String, ByVal reader As TextReader) As TokenStream
            Dim streamStack As New Stack(Of TokenStream)

            streamStack.Push(Me._Analyzer.TokenStream(fieldName, reader))
            If Me._FilterAccents Then
                Dim tempStream As TokenStream = streamStack.Pop
                streamStack.Push(New ASCIIFoldingFilter(tempStream))
            End If
            If Me._FilterToLower Then
                Dim tempStream As TokenStream = streamStack.Pop
                streamStack.Push(New LowerCaseFilter(tempStream))

            End If
            If Me._MinLength > 0 Or Me._MaxLength < Integer.MaxValue Then
                Dim tempStream As TokenStream = streamStack.Pop
                streamStack.Push(New LengthFilter(tempStream, Me._MinLength, Me._MaxLength))
            End If
            If Me._StopWords IsNot Nothing AndAlso Me._StopWords.Length > 0 Then
                Dim tempStream As TokenStream = streamStack.Pop
                streamStack.Push(New StopFilter(True, tempStream, StopFilter.MakeStopSet(Me._StopWords), True))
            End If
            Return streamStack.Pop
        End Function
    End Class
End Namespace

